﻿using HomagChina.CCHCS.DataTypeModel;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using Proxy;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Threading;

namespace HomagChina.CCHS.ViewModels
{
    public class BatchListDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        public RelayCommand UnLoadedCommand { get; }
        public RelayCommand LoadedCommand { get; }
        public RelayCommand ChildItemClickedCommand { get; }

        #endregion Handle

        #region 属性

        private Canvas _ParcelsCanvas = new Canvas();

        public Canvas ParcelsCanvas
        {
            get => _ParcelsCanvas;
            set { _ParcelsCanvas = value; OnPropertyChanged(); }
        }

        private ObservableCollection<BatchList> _BatchList = new ObservableCollection<BatchList>();

        public ObservableCollection<BatchList> BatchList
        {
            get => _BatchList;
            set { _BatchList = value; OnPropertyChanged(); }
        }

        private DispatcherTimer UpdateSessionTimer;

        #endregion 属性

        public BatchListDialogViewModel()
        {
            Query();
            UpdateSessionTimer = new DispatcherTimer();
            UpdateSessionTimer.Interval = TimeSpan.FromMilliseconds(5000);
            UpdateSessionTimer.Tick += (sender, e) => Query();
            LoadedCommand = new RelayCommand(() => UpdateSessionTimer.Start());
            UnLoadedCommand = new RelayCommand(() => UpdateSessionTimer.Stop());
            ChildItemClickedCommand = new RelayCommand(ChildItemClicked);
        }

        private void ChildItemClicked(object obj)
        {
            BatchInfo batchInfo = obj as BatchInfo;
            if (batchInfo != null) new ParcelLayerCreator(ParcelsCanvas, batchInfo.Childs);
        }

        #region 函数

        public void Query()
        {
            if (BatchList.Count > 0) BatchList.Clear();
            BatchList batchItem = new BatchList()
            {
                Name = "GZZH528-250305-04_1",
                BatchCode = "GZZH528-250305-04_1",
                CabinetsNum = "10",
                PackNum = "459",
                WaitCont = "78",
                MissingNum = "08",
                State = "分拣中",
                Childs = new ObservableCollection<BatchInfo>()
            };
            ObservableCollection<LayerInfo> List_BatchInfoModel = new ExcelDataReader().ReadFromExcel();
            //ObservableCollection<LayerInfo> List_BatchInfoModel = new ExcelDataReader().ParcelList;

            List_BatchInfoModel.GroupBy(x => x.RSShelfOrder).ToList().ForEach(x =>
            {
                int Rs = x.First().RSShelfOrder;
                Random ran = new Random();
                batchItem.Childs.Add(
                     new BatchInfo()
                     {
                         Name = $"Parcels {Rs}",
                         ParcelsNo = $"Parcels {Rs}",
                         WaitParcelsNum = ran.Next(0, 12),
                         ParkNum = x.Count(),
                         LackNum = ran.Next(0, 50),
                         Childs = new ObservableCollection<LayerInfo>(List_BatchInfoModel.Where(y => y.RSShelfOrder == Rs).ToList())
                     }
                    );
            });
            BatchList.Add(batchItem);
        }
    }

    #endregion 函数
}